package org.example.orders_learn.config;

import org.example.orders_learn.enums.Events;
import org.example.orders_learn.enums.Status;
import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;

import java.util.EnumSet;

@Configuration
@EnableStateMachine//开启
public class StateMachineConfig extends EnumStateMachineConfigurerAdapter<Status, Events> {

    @Override
    public void configure(StateMachineStateConfigurer<Status, Events> config) throws Exception {
        //指定状态机的初始状态
        config.withStates()
                .initial(Status.STANDBY)
                .states(EnumSet.allOf(Status.class))
                .end(Status.STANDBY);
    }

    @Override
    public void configure(StateMachineTransitionConfigurer<Status, Events> transitions) throws Exception {
        transitions.withExternal()//指定新的状态转换规则
                .source(Status.STANDBY)
                .target(Status.UNPAID)
                .event(Events.CREATE)//指定触发事件
                .and()
                .withExternal()
                .source(Status.UNPAID)
                .target(Status.PAID)
                .event(Events.PAY)
                .and()
                .withExternal()
                .source(Status.PAID)
                .target(Status.SHIPPED)
                .event(Events.SHIPPING)
                .and()
                .withExternal()
                .source(Status.SHIPPED)
                .target(Status.RECEIVED)
                .event(Events.RECEIVE);

    }
}
